import {
  wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()

Page({
  data: {
    datalist: [],
    itemName: '',
    name: '',
    apiurl:''
  },

  onLoad: function(options) { 
    if (options){
      this.setData({
        itemName: options.itemName,
        name: options.name,
        catid: options.catid,
        apiurl: options.apiurl,
        addUrl: options.addUrl || ''
      })
    } 
    this.getlist();
  },

  // 获取列表
  getlist: function() {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + this.data.apiurl,
      method: "post",
      data: {
        keyword: that.data.keywords,
        status: 1,
        pageSize: 10000,
        catId: that.data.catid
      }
    }, function(res) { 
      that.setData({
        datalist: res.list
      })
    })
  },


  //监听搜索
  seacherOn: function(e) {
    var keywords = e.detail
    this.setData({
      keywords
    })
    this.getlist()
  },


  //选择
  choose: function(e) { 
    var item = e.currentTarget.dataset.item 
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面   
    var itemName = this.data.itemName 
    prevPage.setData({
      [itemName]: item,
    })
    wx.navigateBack();
  },


})